/************************************  

DESCRIPTION: This produces the means of the health outcome variables among the matched sample


Datasets Used: 
 (1) LPManalysis
 
Datasets Created: 
  (1-2) PS_YMeans_`i'_fillin 

************************************/  
set more off
capture log close
clear all

global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"
global lpm "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\LPM"

adopath +  "N:/SIL-Common/estout"
adopath +  "N:/SIL-Common/outreg2"
adopath +  "N:/SIL-Common/reghdfe-master/package"
 
log using "$logs/8a.MeansYTables.log", replace
 
	use  "$lpm/LPManalysis.dta"
	drop if year(quarter)==2005
	drop if age<64
	*Various sample restrictions performed, here:
	* (1) limit to age >64
	* (2) limit to quarter >= 184
	* (3) limit to year > 2005


	gen age6574=age<75
	gen age7584=age>=75 & age<85
	gen age8594=age>=85 & age<95
	gen age95plus =age>=95 

	drop if merger_seen==.
	drop if mxdiabetes_p_all ==.
	drop if lor==.
	drop if fs5==.
	drop if urbanrural1==.
	drop if q_of_merger==. & vmerger==1

	capture drop oc_death
	gen oc_death=0
	sort BENE_ID quarter
	by BENE_ID: replace oc_death=1 if BENE_DEATH_DT~=. & _n==_N

	keep if vmerger==1 | vmerger==0

	*Control group/Post-merge/Pre-merge
	gen sampletype=1 if merger_seen==0
	replace sampletype=2 if merger_seen==1 & postm==0
	replace sampletype=3 if merger_seen==1 & postm==1

	*diabetes
	foreach i in "diabcomp1" "diabcomp2" "glaucoma" "death" {
		gen OC3_`i' = oc_`i' if kv_`i'==1 & flag3==1
	}

	*hypertension
	foreach i in "death" "ami" "ihd" "acuteicd8" {
		gen OC2_`i' = oc_`i' if kv_`i'==1 & flag2==1
	}

	forval i=1(1)2 {
	preserve

	collapse (count) n1_death=oc_death n2_death=OC2_death n3_death=OC3_death n_ami=OC2_ami n_ihd=OC2_ihd n_acuteicd8=OC2_acuteicd8  n_diabcomp1=OC3_diabcomp1 n_diabcomp2=OC3_diabcomp2 n_glaucoma=OC3_glaucoma (mean) mn1_death=oc_death  mn2_death=OC2_death mn3_death=OC3_death mn_ami=OC2_ami mn_ihd=OC2_ihd mn_acuteicd8=OC2_acuteicd8 mn_diabcomp1=OC3_diabcomp1 mn_diabcomp2=OC3_diabcomp2 mn_glaucoma=OC3_glaucoma (sd) sd1_death=oc_death sd2_death=OC2_death sd3_death=OC3_death sd_ami=OC2_ami sd_ihd=OC2_ihd sd_acuteicd8=OC2_acuteicd8 sd_diabcomp1=OC3_diabcomp1 sd_diabcomp2=OC3_diabcomp2 sd_glaucoma=OC3_glaucoma  if sex_pat==`i', by(sampletype)

	reshape long n_ n1_ n2_ n3_ mn_ mn1_ mn2_ mn3_ sd_ sd1_ sd2_ sd3_, i(sampletype) j(yvar) str

	rename mn1_ mn1  
	rename mn2_ mn2  
	rename mn3_ mn3  

	rename n1_ n1  
	rename n2_ n2  
	rename n3_ n3  

	rename sd1_ sd1  
	rename sd2_ sd2  
	rename sd3_ sd3  

	reshape long n mn sd , i(sampletype yvar) j(deathvar) str

	drop if n==.

	destring deathvar, force replace
	replace deathvar=2 if yvar=="ami"
	replace deathvar=2 if yvar=="acuteicd8"
	replace deathvar=2 if yvar=="ihd"

	replace deathvar=3 if yvar=="diabcomp1"
	replace deathvar=3 if yvar=="diabcomp2"
	replace deathvar=3 if yvar=="glaucoma"

	replace yvar="_death" if yvar=="death"

	sort sampletype deathvar yvar


	save "$dataProp/PS_YMeans_`i'_fillin.dta", replace
	restore
	}





 